// pages/my/order/orderdetails/orderdetails.js
const app = getApp()
const $ = app.globalData
Page({

	/**
	 * 页面的初始数据
	 */
	data: {

	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		var that = this
		// 根据订单id获取订单详情
		var orderid = options.id
		var quhuo =parseInt(options.quhuo)
		that.setData({
			quhuo:parseInt(quhuo)
		})
		var orderdetail;
		if(quhuo){
			wx.request({
				url:$.AdminUrl+"/YApi/order_get",
				data:{orderid:orderid},
				success:function(data){
					orderdetail = data.data.obj
					that.setData({
						orderdetail: orderdetail
					})
				}
			})
		}else{
			// 获取订单详情
			wx.request({
				url: $.AdminUrl + "/YApi/orderitem_get",
				data: {
					orderitemid: orderid
				},
				success: function(data) {
					orderdetail = data.data.obj
					that.setData({
						orderdetail: orderdetail
					})
					// 根据订单信息判断是快递订单还是自取订单
					if (orderdetail.expressnum) {
						// 快递
						// 通过订单详情请求快递接口
						wx.request({ //快递接口
							url: $.AdminUrl + "/YApi/expressapi",
							data: {
								no: orderdetail.expressnum
							},
							success: (res) => {
								var thislist = JSON.parse(res.data.obj)
								that.setData({
									thislist: thislist
								})
								console.log(thislist)
							}
						})
						// 快递end
					} else {
						// 自取
					}
				}
			})
			// 订单end
		}

	},
	// 申请售后
	shouhou: function(e) {
		var orderid = e.currentTarget.dataset.id
		wx.request({
			url: $.AdminUrl + "/YApi/order_aftersale",
			data: {
				orderid: orderid
			},
			success: function(data) {
				if(data.data.code){
					wx.showToast({
						title: data.data.msg,
						duration: 3000,
					})
				}else{
					wx.showToast({
						icon:'none',
						title: data.data.msg,
						duration: 3000,
					})
				}
			}
		})
	},
	// 确认收货
	shouhuo: function(e) {
		var orderid = e.currentTarget.dataset.id
		wx.request({
			url: $.AdminUrl + "/YApi/order_finish",
			data: {
				customerid:wx.getStorageSync('userinfo').id,
				orderid: orderid
			},
			success: function(data) {
				if(data.data.code){
					wx.showToast({
						title: data.data.msg,
						duration: 3000,
					})
				}else{
					wx.showToast({
						icon:'none',
						title: data.data.msg,
						duration: 3000,
					})
				}
			}
		})
	},// 分享转发
		onShareAppMessage: function() {
			var userinfo = wx.getStorageSync('userinfo')
			return {
				title: '优悦购生活管家',
				path: '/pages/index/index?userid='+userinfo.loginname,
				success: (res) => {
					console.log("转发成功", res);
				},
				fail: (res) => {
					console.log("转发失败", res);
				}
			}
		}
})
